package com.tianhui.consignortwofei.mvp.ui.activity.auth;

import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonFragment;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.itemView.RegionItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.enty.BusinessLicenseInfo;
import com.tianhui.consignortwofei.mvp.model.enty.ConsignorInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.IdCardInfo;
import com.tianhui.consignortwofei.other.PictureType;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.BusinessLicenseStrategy;
import com.tianhui.consignortwofei.other.auth.stragety.IdCardBackStrategy;
import com.tianhui.consignortwofei.other.auth.stragety.IdCardFrontStrategy;
import com.tianhui.consignortwofei.widget.AuthView;
import com.xuexiang.xutil.net.JsonUtil;

import butterknife.OnClick;

public class ConsignorAuthActivity extends BaseAuthActivity {

    @Override
    public String getToolbarTitle() {
        return "货主认证";
    }

    @Override
    public void init() {
        super.init();
        mConsignorAuthInfo.istatus = 1;

        getPresenter().requestConsignorAuthenticationInfo(null, true);

        initListener();

    }


    @Override
    public void showConsignorAuthenticationInfo(ConsignorInfo info) {
        mConsignorAuthInfo = info;
        boolean isAudit = info.isaudit == 1;
        isShowConsignorCommitButton(isAudit);
        showConsignorPictureInfo(info);
        setClickable(!isAudit);
        setInputAble(!isAudit);
        showConsignorInfo(info);
    }

    private void setInputAble(boolean isInputAble) {
//        legalIdNumberInputItemView.setItemEnable(isInputAble);
        taxAuthorityInputItemView.setItemEnable(isInputAble);
    }

    private void showConsignorInfo(ConsignorInfo info) {

        contractPhoneInputItemView.setItemEnable(false);

        legalNameInputItemView.setContent(info.legalperson);
        legalIdNumberInputItemView.setContent(info.customercardid);
        legalPhoneInputItemView.setContent(info.legaltel);
        consignorNameInputItemView.setContent(info.customername);
        merchantNameInputItemView.setContent(info.customerab);
        merchantPhoneInputItemView.setContent(info.servicePhoneNo);
        contractNameInputItemView.setContent(info.contacts);
        contractPhoneInputItemView.setContent(info.customertel);
        chargeNameInputItemView.setContent(info.principalPerson);
        chargePhoneInputItemView.setContent(info.principalMobile);
        feeInputItemView.setContent(info.handlingfee);
        addressRegionItemView.setContent(mConsignorAuthInfo.placeprovince + " " + mConsignorAuthInfo.placecity + " " + mConsignorAuthInfo.placecounty);
        taxAuthorityInputItemView.setContent(mConsignorAuthInfo.zgswjg);

    }

    protected void isShowConsignorCommitButton(boolean isShow) {
        if (isShow) { //认证通过
            consignorCommitButton.setVisibility(View.GONE);
        } else {
            consignorCommitButton.setVisibility(View.VISIBLE);
        }
    }

    private void setClickable(boolean isClickable) {
        idCardFrontAuthView.setIsClickable(isClickable);
        idCardBackAuthView.setIsClickable(isClickable);
        businessLicenseAuthView.setIsClickable(isClickable);
        addressRegionItemView.setItemClickable(isClickable);
    }

    protected void showConsignorPictureInfo(ConsignorInfo info) {

        boolean isAudit = info.isaudit == 1;

        if (!TextUtils.isEmpty(info.idcardtphoto)) {
            idCardFrontAuthView.setImageWithId(info.idcardtphoto);
            if (isAudit) {
                idCardFrontAuthView.showSuccessStatus();
            }
            isIdCardFrontSuccess = true;
        }
        if (!TextUtils.isEmpty(info.idcardfphoto)) {
            idCardBackAuthView.setImageWithId(info.idcardfphoto);
            if (isAudit) {
                idCardBackAuthView.showSuccessStatus();
            }

            isIdCardBackSuccess = true;
        }
        if (!TextUtils.isEmpty(info.businesslicense)) {
            businessLicenseAuthView.setImageWithId(info.businesslicense);
            if (isAudit) {
                businessLicenseAuthView.showSuccessStatus();
            }
            isBusinessLicenseSuccess = true;
        }
    }


    protected void initListener() {

        idCardFrontAuthView.setOnAuthPictureViewClickListener(new AuthView.OnAuthPictureViewClickListener() {
            @Override
            public void onPictureViewClick(View v) {

                IdCardFrontStrategy idCardStrategy = (IdCardFrontStrategy) pictureStrategyFactory.createStrategy(IdCardFrontStrategy.class);
                pictureContext = new PictureContext(idCardStrategy);
                pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<IdCardInfo>() {
                    @Override
                    public void selectPhotoSuccess(SelectPhotoInfo<IdCardInfo> selectPhotoInfo) {

                        processIdCardFrontSuccessResult(selectPhotoInfo);
                    }

                    @Override
                    public void selectPhotoFailed() {
                        idCardFrontAuthView.showFailedStatus();
                    }
                });

            }
        });

        idCardBackAuthView.setOnAuthPictureViewClickListener(new AuthView.OnAuthPictureViewClickListener() {
            @Override
            public void onPictureViewClick(View v) {
                IdCardBackStrategy idCardBackStrategy = (IdCardBackStrategy) pictureStrategyFactory.createStrategy(IdCardBackStrategy.class);
                pictureContext = new PictureContext(idCardBackStrategy);
                pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<IdCardInfo>() {
                    @Override
                    public void selectPhotoSuccess(SelectPhotoInfo<IdCardInfo> selectPhotoInfo) {
                        processIdCardBackSuccessResult(selectPhotoInfo);

                    }

                    @Override
                    public void selectPhotoFailed() {
                        idCardBackAuthView.showFailedStatus();
                    }
                });

            }
        });
//
        businessLicenseAuthView.setOnAuthPictureViewClickListener(new AuthView.OnAuthPictureViewClickListener() {
            @Override
            public void onPictureViewClick(View v) {
                BusinessLicenseStrategy businessLicenseStrategy = (BusinessLicenseStrategy) pictureStrategyFactory.createStrategy(BusinessLicenseStrategy.class);
                pictureContext = new PictureContext(businessLicenseStrategy);
                pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<BusinessLicenseInfo>() {
                    @Override
                    public void selectPhotoSuccess(SelectPhotoInfo<BusinessLicenseInfo> selectPhotoInfo) {
                        processBusinessLicenseSuccessResult(selectPhotoInfo);
                    }

                    @Override
                    public void selectPhotoFailed() {
                        businessLicenseAuthView.showFailedStatus();
                    }
                });

            }
        });
        addressRegionItemView.setOnRegionCallback(new RegionItemView.OnRegionSelectedCallback() {
            @Override
            public void regionCallback(RegionInfo info) {
                mConsignorAuthInfo.placeprovince = info.province;
                mConsignorAuthInfo.placecity = info.city;
                mConsignorAuthInfo.placecounty = info.county;
                mConsignorAuthInfo.areacode = info.code;
            }
        });
    }


    //营业执照
    protected void processBusinessLicenseSuccessResult(SelectPhotoInfo<BusinessLicenseInfo> selectPhotoInfo) {
        if (!isDestroyed() && !isFinishing()) {
            mConsignorAuthInfo = mAuthInfoProcess.processBusinessLicenseInfo(selectPhotoInfo.resultInfo, mConsignorAuthInfo);
            mConsignorAuthInfo.businesslicense = selectPhotoInfo.pictureId;
            businessLicenseAuthView.setImageWithPath(selectPhotoInfo.filePath);
            businessLicenseAuthView.showSuccessStatus();
            commitSuccess(PictureType.TYPE_BUSINESS_LICENSE);
            showRecognitionBusinessLicenseInfo(mConsignorAuthInfo);
//            requestDriverInfoCommit(mDriverAuthInfo, PictureType.TYPE_DRIVE_LICENSE);
        }
    }

    private void showRecognitionBusinessLicenseInfo(ConsignorInfo consignorAuthInfo) {
        legalNameInputItemView.setContent(consignorAuthInfo.legalperson);
        consignorNameInputItemView.setContent(consignorAuthInfo.customername);
        merchantNameInputItemView.setContent(consignorAuthInfo.customername);
    }

    protected void processIdCardBackSuccessResult(SelectPhotoInfo<IdCardInfo> selectPhotoInfo) {
        if (!isDestroyed() && !isFinishing()) {
            mConsignorAuthInfo.idcardfphoto = selectPhotoInfo.pictureId;
            idCardBackAuthView.setImageWithPath(selectPhotoInfo.filePath);
            idCardBackAuthView.showSuccessStatus();
            commitSuccess(PictureType.TYPE_BACK);
//            requestDriverInfoCommit(mDriverAuthInfo, PictureType.TYPE_BACK);
        }
    }

    protected void processIdCardFrontSuccessResult(SelectPhotoInfo<IdCardInfo> selectPhotoInfo) {
        if (!isDestroyed() && !isFinishing()) {
            mConsignorAuthInfo = mAuthInfoProcess.processIdCardFrontInfo(selectPhotoInfo.resultInfo, mConsignorAuthInfo);
            mConsignorAuthInfo.idcardtphoto = selectPhotoInfo.pictureId;
            idCardFrontAuthView.setImageWithPath(selectPhotoInfo.filePath);
            idCardFrontAuthView.showSuccessStatus();
            showRecognitionIdCardFrontInfo();
            commitSuccess(PictureType.TYPE_FRONT);

            contractNameInputItemView.setContent(mConsignorAuthInfo.contacts);
            chargeNameInputItemView.setContent(mConsignorAuthInfo.principalPerson);


//            requestDriverInfoCommit(mConsignorAuthInfo, PictureType.TYPE_FRONT);
        }
    }

    private void showRecognitionIdCardFrontInfo() {
        legalNameInputItemView.setContent(mConsignorAuthInfo.legalperson);
        legalIdNumberInputItemView.setContent(mConsignorAuthInfo.customercardid);
    }


    @Override
    public CommonFragment createFragment() {
        return null;
    }


    @OnClick(R.id.layout_consignor_auth_commitButton)
    public void commit() {
        if (!checkConsignorInfoProgress()) {
            return;
        }
        String legalName = legalNameInputItemView.getContent();
        String legalIdCardNumber = legalIdNumberInputItemView.getContent();
        String legalPhone = legalPhoneInputItemView.getContent();
        String merchantName = merchantNameInputItemView.getContent();
        String merchantPhone = merchantPhoneInputItemView.getContent();
        String contract = contractNameInputItemView.getContent();
        String chargeName = chargeNameInputItemView.getContent();
        String chargePhone = chargePhoneInputItemView.getContent();
        String fee = feeInputItemView.getContent();
        String taxAuthority = taxAuthorityInputItemView.getContent();
//        if (TextUtils.isEmpty(legalName)) {
//            showToast("请输入法人姓名");
//            return;
//        }
//
//        if (TextUtils.isEmpty(legalIdCardNumber)) {
//            showToast("请输入法人身份证号");
//            return;
//        }
//        if (TextUtils.isEmpty(legalPhone)) {
//            showToast("请输入法人手机号");
//            return;
//        }
//        if (TextUtils.isEmpty(merchantName)) {
//            showToast("请输入商户简称");
//            return;
//        }
//        if (TextUtils.isEmpty(merchantPhone)) {
//            showToast("请输入商户客服电话");
//            return;
//        }
//        if (TextUtils.isEmpty(contract)) {
//            showToast("请输入联系人");
//            return;
//        }
//        if (TextUtils.isEmpty(chargeName)) {
//            showToast("请输入负责人");
//            return;
//        }
//        if (TextUtils.isEmpty(chargePhone)) {
//            showToast("请输入负责人手机号");
//            return;
//        }
//        if (TextUtils.isEmpty(fee)) {
//            showToast("请输入费率");
//            return;
//        }
//        if (TextUtils.isEmpty(mConsignorAuthInfo.placeprovince) || TextUtils.isEmpty(mConsignorAuthInfo.placecity)) {
//            showToast("请选择省市县");
//            return;
//        }
        mConsignorAuthInfo.legalperson = legalName;
        mConsignorAuthInfo.customercardid = legalIdCardNumber;
        mConsignorAuthInfo.legaltel = legalPhone;
        mConsignorAuthInfo.customerab = merchantName;
        if (TextUtils.isEmpty(merchantPhone)){
            merchantPhone = SharedPreferenceManager.getInstance().getPhone();
        }
        mConsignorAuthInfo.servicePhoneNo = merchantPhone;
        mConsignorAuthInfo.contacts = contract;
        mConsignorAuthInfo.principalPerson = chargeName;
        mConsignorAuthInfo.principalMobile = chargePhone;
        mConsignorAuthInfo.handlingfee = fee;
        mConsignorAuthInfo.zgswjg = taxAuthority;
        LogUtil.print(JsonUtil.toJson(mConsignorAuthInfo));
        getPresenter().requestConsignorInfoCommit(mConsignorAuthInfo, 0);
    }

    @Override
    protected boolean isShowFinishTipDialog() {
        return !checkConsignorInfoProgress();
    }

    @Override
    protected String getTipMessage() {
        return "资料信息尚未上传完成";
    }


    @Override
    protected void onDestroy() {
        clearDriverAuthView();
        super.onDestroy();
    }

    private void clearDriverAuthView() {
        if (idCardFrontAuthView != null) {
            idCardFrontAuthView.setOnAuthPictureViewClickListener(null);
        }
        if (idCardBackAuthView != null) {
            idCardBackAuthView.setOnAuthPictureViewClickListener(null);
        }
        if (businessLicenseAuthView != null) {
            businessLicenseAuthView.setOnAuthPictureViewClickListener(null);
        }
    }


}
